const {
  saveNodes,
  eraseNodeById,
  getNodes,
  eraseNodes,
  batchAddNodes
} = require("../services/nodes.service.js");
const err = require("../constant/err.type.js");

class NodesController {
  async saveNode(ctx, next) {
    var res;
    try {
      const { ...Nodes } = ctx.request.body;
      if (!Nodes.nodeId) {
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: -1, msg: "保存节点失败", data: "" }),
          ctx
        );
        return;
      }
      res = await saveNodes(Nodes);
      if (!res) {
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: -1, msg: "保存节点失败", data: "" }),
          ctx
        );
        return;
      } else {
        ctx.status = 200;
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: 0, msg: "ok", data: res }),
          ctx
        );
        return;
      }
    } catch (error) {
      console.log(error);
      ctx.app.emit(
        "error",
        err.setDefaultValue({
          code: -1,
          msg: "保存节点时出现错误，代码为" + error,
          data: "",
        }),
        ctx
      );
      return;
    }
  }
  async createNode(ctx, next) {
    var res;
    try {
      const { ...Nodes } = ctx.request.body;
      res = await saveNodes(Nodes);
      if (!res) {
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: -1, msg: "创建节点失败", data: "" }),
          ctx
        );
        return;
      } else {
        ctx.status = 200;
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: 0, msg: "ok", data: res }),
          ctx
        );
        return;
      }
    } catch (error) {
      console.log(error);
      ctx.app.emit(
        "error",
        err.setDefaultValue({
          code: -1,
          msg: "保存节点时出现错误" + error,
          data: "",
        }),
        ctx
      );
      return;
    }
  }
  async batchAddNodes(ctx,next){
    var res;
    var rs;
    try {
      const {linkName,arr} = ctx.request.body;
      if(!(arr instanceof Array) || arr.length == 0){
        ctx.app.emit("error",err.setDefaultValue({code:-1,msg:"导入数据错误，数据不能为空",data:""}),ctx);
        return;
      }
      rs = await eraseNodes({linkName});
      res = await batchAddNodes(arr);
      if(!res){
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: -1, msg: "导入数据失败", data: "" }),
          ctx
        );
        return;
      }else{
        ctx.status = 200;
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: 0, msg: "ok", data: res }),
          ctx
        );
        return;
      }
    } catch (error) {
      console.log(error);
      ctx.app.emit(
        "error",
        err.setDefaultValue({
          code: -1,
          msg: "导入数据出错" + error,
          data: "",
        }),
        ctx
      );
      return;
    }
  }  
  async eraseNode(ctx, next) {
    var res;
    var { linkName } = ctx.request.body;
    if (!linkName) {
      ctx.app.emit(
        "error",
        err.setDefaultValue({ code: -1, msg: "节点ID错误", data: "" }),
        ctx
      );
      return;
    }
    try {
      res = await eraseNodes({linkName});
      if (!res) {
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: -1, msg: "删除节点失败", data: "" }),
          ctx
        );
        return;
      } else {
        ctx.status = 200;
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: 0, msg: "ok", data: res }),
          ctx
        );
      }
    } catch (error) {
      console.log(error);
      ctx.app.emit(
        "error",
        err.setDefaultValue({
          code: -1,
          msg: "删除节点时出现错误" + error,
          data: "",
        }),
        ctx
      );
      return;
    }
  }
  async getNodes(ctx, next) {
    var { nodeId,linkName, nodeName,nodeType } = ctx.request.body;
    var whereOpt = {};
    var res;
    try {
      nodeId > 0 && Object.assign(whereOpt, { nodeId });
      nodeName && Object.assign(whereOpt, { nodeName });
      nodeType && Object.assign(whereOpt, { nodeType }); 
      linkName && Object.assign(whereOpt, { linkName }); 
      res = await getNodes(whereOpt);
      if (!res) {
        ctx.app.emit("error", err.companySearchError, ctx);
        return;
      }
      ctx.status = 200;
      ctx.app.emit(
        "error",
        err.setDefaultValue({ code: 0, msg: "ok", data: res }),
        ctx
      );
    } catch (error) {
      console.log(error);
      ctx.app.emit("error", err.companySearchError, ctx);
      return;
    }
  }
}

module.exports = new NodesController();
